<?php
defined('_O2PHP_EXEC') or die();
# 版本兼容控制
if (version_compare(phpversion(), "5.1.0", "<")) die('PHP version can not be less than 5.1.0 ');
# 得到逻辑处理类
function &module($modulesName,$error_msgBox=true){
	static $instance = array();
	$modulesName = strtolower($modulesName);
	if (!isset($instance[$modulesName])) {
	  $modulesName = $modulesName.'_model';
		if(!file_exists(EMVC_PATH_APP.DS.'models'.DS.$modulesName.'.php')){
			if($error_msgBox){
				msgBox('models类不存在：'.$modulesName,'(-1)');
			}else{
				return false;
			}	
		}
		load('models.' . $modulesName,EMVC_PATH_APP);
		$adapter = $modulesName;
		$instance[$modulesName] = new $adapter ();
	}
	return $instance[$modulesName];
}
# alert形式提示，然后跳转指定页面
function msgAlert($message = '',$gotourl = 'index.php?'){
	$file = EMVC_PATH_LIBRARIES.'core'.DS.'utils' . DS . 'msgalert.php';
	if (file_exists($file)) {
		include ($file);
		exit();
	} else {
		trigger_error($message, E_USER_ERROR);
	}
}
# 错误提示
function msgBox($error = '', $gotourl = '(-1)', $time = 5 ) {
	if($_GET['reqtype'] === "json"){
		//exit('{"success":"0","msg":"'.$error.'"}');
		exit($error);
	}
	if ($gotourl==='(-1)' || $gotourl==='-1') {
		$gotourl = 'javascript:history.go(-1)';
		$gotourl_js = 'history.go(-1)';
	} elseif($gotourl==='window.close' || $gotourl==='window.close()' || $gotourl==='close'  || $gotourl==='close()' ) {
		$gotourl = 'javascript:window.close()';
		$gotourl_js='window.close()';
	}else{
		$gotourl_js = ((('' . 'self.location.href=\'') . $gotourl) . '\';');
	}
	$file = EMVC_PATH_LIBRARIES.'core'.DS.'utils' . DS . 'msgbox.php';
	if (file_exists($file)) {
		include ($file);
		exit();
	} else {
		trigger_error($error, E_USER_ERROR);
	}
}
# 得到数据库连接   PDO的是支持事物
function &getDBO($templet = '') {
	static $instance = array();
	if($templet === '') $templet = getCfg('db_default','default');
	if (!isset($instance[$templet])) {
		$conf = getCfg('db_templet');
		$options = $conf[$templet];
		if(getCfg('db_pdo',true)){
			load('core.database.db_pdo');
			$instance[$templet] = new Db ($templet,$options);
		}else{
			load('core.database.db_old');
			$instance[$templet] = new EDatabaseMySQL($options);
		}
	}
	return $instance[$templet];
}
# 得到config变量
function &getCfg($key=null,$default=null){
	static $config;
	if(!isset($config)){
		include_once(EMVC_PATH_ROOT . 'config.php');
		$config=new EConfig();
	}
	if($key===null)return $config;
	if(isset($config->$key))return $config->$key;
	else return $default;
}
# 加载json操作对象
function &loadjson(){
	static $instance;
	if(!isset($instance)){
		load('core.json.json');
		$instance=new Services_JSON();
	}
	return $instance;
}
# 加载php文件
function load($class,$base=EMVC_PATH_LIBRARIES) {
	static $files=array();
	if(!isset($files[$class]) || !($files[$class]==true))
	{
		$file = $base . str_replace('.', DS, $class) . '.php'; //substr($class, 2)
		$inc=include($file);
		if($inc){
			$files[$class]=true;
			return $inc;
		}else{
			return false;
		}
	}else{
		return true;
	}
}
# 请求变量进行处理函数系列
function addslash_deep($val,$force=false) {
	if (!get_magic_quotes_gpc() || $force) {
		if (is_array($val)) {
			foreach ($val as $key => $value) {
				if (is_array($value)) {
					$val[$key] = addslash_deep($value,$force);
				} else {
					$val[$key] = addslashes($value);
				}
			}
		}	else {
			$val = addslashes($val);
		}
	}
	return $val;
}
function addslash_json($val)
{
	return addslash_deep(jsonToArray($val),true);
}
function jsonToArray($val){
	if(is_object($val)){
		$val = get_object_vars($val);
	} 
	foreach ($val as $key => $value) {
		if (is_array($value) || is_object($value)) {
			$val[$key] = jsonToArray($value);
		} else {
			//$val[$key] = addslashes($value);
		}

	}
	return $val;
}
# 加载页面请求解析对象
function loadRequest(){
	static $instance;
	if(!isset($instance)){
		$options['url_mode']=getCfg('url_mode',1);
		$options['url_q']=getCfg('url_q','url');
		$options['url_spliter']=getCfg('url_spliter','-');
		load('core.environment.request');
		$instance=new ERequest($options);
	}
	return $instance;
}
function registry(){
	static $instance;
	if(!isset($instance)){
		load('core.etpl.registry');
		$instance=new ERegistry();
	}
	return $instance;
}
function &loadUser(){
	static $instance;
	if(!isset($instance)){
		load('core.acl.user');
		$instance=&EUser::singleton();
	}
	return $instance;
}
function &env($key, $default = null) {
	$val = null;
	if (isset ($_SERVER[$key])) {
		$val = & $_SERVER[$key];
	}
	elseif (isset ($_ENV[$key])) {
		$val = & $_ENV[$key];
	}
	elseif (getenv($key) !== false) {
		$val = & getenv($key);
	}
	if ($val !== null) {
		return $val;
	} else {
		return $default;
	}
}
# 得到跟目录
function urlRoot(){
	static $url;
	if(!isset($url)){
		$url=dirname(env('SCRIPT_NAME'));
		if(strlen($url)>1){
			$url.='/';
		}else{
			$url='/';
		}
	}
	return $url;
}
# 取模板URL
function getThemesDir(){
	return urlRoot();
}


# 编码URL
function encodeUrlArgs($args) {
	if(empty($args))return '';
	$str = '';
	if(!is_array($args))$args=(array)$args;
	foreach ($args as $key => $value) {
		$str .= '&' . rawurlencode($key) . '=' . rawurlencode($value);
	}
	return substr($str,0);
  
}
# 得到url全路径
function url($module='',$action='',$args=array(),$abs=false,$ctrl=''){
	if($module === '') $module=getCfg('defaultModule');
	if($action === '') $action=getCfg('defaultAction');
	if($ctrl === '')   $sctrl = 'index.php';
	$url=$ctrl.'?'.getCfg('ctl_name','m').'='.$module.'&'.getCfg('act_name','a').'='.$action.encodeUrlArgs($args);
	//debug(urlRoot().$url);
	if($abs)$url=urlRoot().$url;
	return $url;
}
function url_a($name,$module='',$action='',$args=array(),$abs=false,$ctrl=''){
	return '<a href="'.url($module,$action,$args,$abs,$ctrl).'" >'.$name.'</a>';
}
# 得到uri后缀系列
function sefUri($module='',$action='',$args=array(),$spliter='-',$type=''){
	if($module === '') $module=getCfg('defaultModule');
	if($action === '') $action=getCfg('defaultAction');
	if($type === '')   $type=getCfg('defaultReqType');
	if($args){
		$arg='';
		if(!is_array($args))$args=(array)$args;
		foreach($args as $key=>$value){
			$arg.=$spliter.$key.$spliter.$value;
		}
		$arg=rtrim($arg,$spliter);
	}else $args='';
	$url=$module.$spliter.$action.$arg.'.'.$type;
	return $url;
}
/*
**弹出信息
*/
function alert($msg)
{
	echo "<script type='text/javascript'>";
	echo "alert('".$msg."')";
	echo "</script>"; 
}
/*
**脚本的目录转换
*/
function jumpTo($page)
{
	echo "<script type='text/javascript'>";
	echo "document.location.href = '".$page."'";
	echo "</script>"; 
}
?>